﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Sql;
using System.Data.Odbc;
using System.Threading;

namespace Data.Odbc.Core
{
    public class Helper : IDisposable
    {
        private string connStr = string.Empty;
        private int timeout = 30;
        private System.Data.Odbc.OdbcConnection connection;
        private System.Data.Odbc.OdbcCommand cmd;

        public Helper(string connStr, int timeout = 30)
        {
            connection = new OdbcConnection(connStr);
            connection.ConnectionTimeout = timeout;
            try
            {
                connection.Open();
            }
            catch (OdbcException e)
            {
                //Log
            }
        }

        public void Close()
        {
            if (cmd != null)
            {
                cmd.Cancel();
            }
            if (connection != null)
            {
                connection.Close();
            }
        }

        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>int:受影响行数</returns>
        public int Exec(string sql)
        {
            int result = 0;
            try
            {
                if (connection != null)
                {
                    cmd = connection.CreateCommand();
                    if (cmd != null)
                    {
                        cmd.CommandText = sql;
                        result = cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        //Log CMD Error
                    }
                }
                else
                {
                    //Log Connectioin Error
                }
                return result;
            }
            catch (Exception e)
            {
                //Log
                return result;
            }
            finally
            {
                Close();
            }
        }


        public IQueryable Query(string sql)
        {
            IQueryable result = null;
            try
            {
                if (connection != null)
                {
                    cmd = connection.CreateCommand();
                    if (cmd != null)
                    {
                        cmd.CommandText = sql;
                        result = cmd.ExecuteReader().AsQueryable();
                    }
                    else
                    {
                        //Log CMD Error
                    }
                }
                else
                {
                    //Log Connectioin Error
                }
                return result;
            }
            catch (Exception e)
            {
                //Log
                return result;
            }
            finally
            {
                Close();
            }
        }

        /// <summary>
        /// 事务处理
        /// </summary>
        /// <param name="sqls">sql语句</param>
        /// <returns>boolen</returns>
        public bool Trans(IList<string> sqls)
        {
            throw new NotImplementedException();
        }

        protected virtual void Dispose(bool disposing)
        {
            Close();
            if (cmd != null)
            {
                cmd.Dispose();
            }
            if (connection != null)
            {
                connection.Dispose();
            }
            GC.SuppressFinalize(this);
        }
        public void Dispose()
        {
            Dispose(true);
        }
    }
}
